# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import os
import sys
import re
from datetime import datetime
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    pass



def read_log_file(fileName:str):
    with open(fileName, 'r', encoding='utf-8') as file:
        pattern = r":\s+"
        head_pattern = r"\s+"
        logLines = file.readlines

        date_string = "2022-08-15 12:34:56"
        date_format = "%Y-%m-%d %H:%M:%S.%f"
        #记录字典
        record={}

        for line in file:
            #先找到左边第一个: 用来区分tag 以及 log内容 
            result = re.split(pattern, line)
            if(len(result)==2):
                head = result[0]
                content = result[1]
                # 对head进行分割获取 时间 进程号 线程号  level tag 并计数
                infos = re.split(head_pattern,head)
                # 进程号 等数据
                time_date=infos[0]
                time_millis=infos[1]
                pid = infos[2]
                tid = infos[3]
                level = infos[4]
                tag = infos[5]

                item = record.get(pid)
                #创建新的记录条目
                if not item:
                    item = {}

                #记录日志开始时间
                start_time = item.get('startTime')
                if not start_time:
                    item['startTime']=f"{time_date} {time_millis}"

                item['endTime']=f"{time_date} {time_millis}"


                from_time = item.get('startTime')
                to_time = item.get('endTime')

                ft = datetime.strptime(from_time, "%Y-%m-%d %H:%M:%S.%f")
                tt = datetime.strptime(to_time, "%Y-%m-%d %H:%M:%S.%f")

                item['duration']=tt-ft
            

                #记录进程日志总行数
                totalLines = item.get('totalLines')
                if not totalLines:
                    item['totalLines']=1
                else:
                    item['totalLines'] = totalLines+1
                
                # 根据tag 来分别记录
                tags = item.get('tags')
                if not tags:
                    tags={}

                current_tag  =tags.get(tag)

                if not current_tag:
                    current_tag={}
                
                # 当前tag数量
                tag_count = current_tag.get('count')
                if not tag_count:
                    current_tag['count']=1
                else:
                    current_tag['count']=tag_count+1

                leng = current_tag.get('max_len')
                new_len = len(content)    
                if not leng:
                    current_tag['max_len']=new_len
                else:
                    current_tag['max_len'] = new_len if new_len > leng else leng
                

                tags[tag]=current_tag
                item['tags']=tags
                record[pid]=item


        for key in record.keys():
            print(key,record.get(key))


read_log_file('logcat.txt')
